package com.quanyan.club.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.quanyan.api.APIResponse;
import com.quanyan.club.entity.db.TbOrder;
import com.quanyan.club.entity.db.TbOrderExample;
import com.quanyan.club.entity.vo.resp.RespClubActivity;
import com.quanyan.club.mapper.TbOrderMapper;
import com.quanyan.club.service.ActivityAccountService;
import com.quanyan.club.service.ActivityCRUDService;
import com.quanyan.club.service.ActivityCallBackService;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yue.yuan on 2017/1/4.
 */
@Service("activityCallBackService")
public class ActivityCallBackServiceImpl implements ActivityCallBackService {

    private static final Logger logger = LoggerFactory.getLogger(ActivityCallBackServiceImpl.class);

    @Autowired
    private ActivityCRUDService activityCRUDService;
    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private ActivityAccountService activityAccountService;

    /**
     * 支付系统退款后，通过MQ发送退款处理结果通知，收到通知后处理订单状态
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean cancelOrderNotifyCallback(String businessOrderNo) {
        //1. 查询订单信息，俱乐部活动信息
        TbOrderExample orderExample = new TbOrderExample();
        orderExample.createCriteria()
                .andOrderNoEqualTo(businessOrderNo)
                .andOrderStatusEqualTo((byte) 4);    //取消，退款中的订单
        List<TbOrder> orders = tbOrderMapper.selectByExample(orderExample);
        if (orders == null || orders.size() <= 0) {
            return false;
        }
        TbOrder order = orders.get(0);
        int activityId = order.getActivityId();
        int orderUid = order.getOrderUid();
        logger.warn("1 要更新状态的订单：" + JSON.toJSONString(order));

        RespClubActivity respClubActivity = activityCRUDService.selectClubActivityById(activityId);
        int clubId = respClubActivity.getClubId();

        //2. 将抵扣的俱乐部余额退回到俱乐部成员账户
        activityAccountService.refundBalanceAmount(order, respClubActivity.getName(), clubId);
        //5. 更改订单状态
        order.setOrderStatus((byte) 5); //用户取消，退款完成
        int ret = tbOrderMapper.updateByPrimaryKeySelective(order);
        logger.warn("5  更改订单状态为5（已退款），返回：" + ret);
        //6.将此人剔除活动聊天组
        if (ret == Constants.IS_YES) {
            /*boolean result = cppService.addOrDelIMGroupMember(respClubActivity.getUid(),
                    activityId, respClubActivity.getGroupId(),
                    Lists.newArrayList(orderUid), false);
            logger.warn("6 将此人{}剔除活动聊天组{}，返回：{}", order.getOrderUid(), respClubActivity.getGroupId(), result);
            cppService.pushCancelActivityOrder(respClubActivity, orderUid);
            cppService.pushRefundActivityOrder(respClubActivity, orderUid, businessOrderNo);*/
            return true;
        }
        return false;
    }
}
